﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace WorkRecoder
{
    public partial class AddTimeForm : Form
    {
        public static bool Check(DateTime last)
        {
            if ((DateTime.Now - last) < new TimeSpan(0, 10, 0))
            {
                var dlg = new AddTimeForm();
                dlg.LastTime = last;
                if (dlg.ShowDialog() == DialogResult.OK)
                    return true;
            }
           return  false;
        }

        public DateTime LastTime
        {
            get;
            set;
        }
        public XElement Task
        {
            get;
            protected set;
        }
        public AddTimeForm()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("You should write something in common!");
                return;
            }
            if (dateTimePicker2.Value > DateTime.Now)
            {
                MessageBox.Show("Time Error!");
                return;
            }
            DateTime dt = dateTimePicker1.Value;
            var endtime = dateTimePicker2.Value;

            var span = endtime - dt;
            var tmp = dt.ToShortTimeString() + "-" +
                endtime.ToShortTimeString() + "=" + span.Hours + "h" + span.Minutes + "min";
            var date = new string[] { tmp, comboBox1.Text, textBox1.Text };
            var t = new ListViewItem(date);
            t.Tag = span;
            listView1.Items.Add(t);

            if (Task == null)
                Task = new XElement("Task");
            Task.Add(new XElement("Row", new XAttribute("TimeSpan", span.ToString()),
                new XAttribute("EndTime", endtime.ToString()),
                new XElement("Time", date[0]),
                new XElement("Item", date[1]),
                new XElement("Common", date[2])));

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

            dateTimePicker1.Value = dateTimePicker2.Value;
            dateTimePicker2.Value = DateTime.Now;

            textBox1.Text = "";
        }

        [PreEmptive.Attributes.Feature("AddTimeLoad")]
        private void Form1_Load(object sender, EventArgs e)
        {
            var ini = new jks.IniFiles("WorkRecoder.ini");
            var tmp = ini.ReadString("WorkRecoder", "Item", " ");
            comboBox1.DataSource = tmp.Split(';');

            dateTimePicker1.Value = LastTime;
            dateTimePicker2.Value = DateTime.Now;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
